layui.use(['form', 'table'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table;
    let searchFieldsetStatus=localStorage.getItem("searchFieldsetStatus");
    if(searchFieldsetStatus=='0')
    {
        $(".search-display-btn").attr("show","0");
        $(".search-display-btn").html('<i class="fa fa-angle-down"></i>展开');
        $($('.layui-form')[0]).hide();
    }
    let u = localStorage.getItem("a@m");
    let loginU = {};
    if (u == undefined) {
        // layer.msg('请登录', {icon: 1, time: 500}, function () {
        //     window.location = 'logout';
        // });
    } else {
        loginU = decryptLocalStorage(u);
    }
    setTableHeight();
    let tableUrl = baseUrl + "admin/v1/app/page?token=" + loginU.token;
    table.render({
        elem: '#currentTableId',
        url: tableUrl,loginUserType:loginU.userType,
        toolbar: '#toolbarPanel',
        defaultToolbar: [, 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            {type: "numbers", align: "center", title: '序号', width: 60, align: "center"},
            {field: 'appName', align: "center", title: 'APP名称'},
            {field: 'newAppVersionCode', align: "center", title: '版本编号'},
            {
                field: 'newAppVersionName', align: "center", title: '版本名称', align: 'center', sort: true,
                templet: function (data) {
                    return "<a href='/admin/v1/common/downloadApk?filePath=" + data.newAppUrl + "' target='_blank' style='color:blue;'>" + data.newAppVersionName + "</a>";
                }
            },
            {field: 'newAppUpdateDesc', title: '版本描述', align: 'center', sort: true},
            {field: 'newAppReleaseTime', title: '操作时间', align: 'center', sort: true},
            {title: '操作', minWidth: 220, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100, 200, 300, 500],
        limit: 20,
        page: true,
        skin: 'line',parseData: function (res) {
            for (let i in res.data) {
                res.data[i].loginUserType = loginU.userType;
            }
            return res;
        },
        done: function (res, curr, count) {
            if (res.code === -1) {
                layer.msg('请登录', {icon: 1, time: 500}, function () {
                    //window.location = 'logout';
                });
                return;
            }
            setTableHeight();
        }
    });

    // 监听搜索操作
    let keyWork = "{}";
    form.on('submit(data-search-btn)', function (data) {
        keyWork = JSON.stringify(data.field);
        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                keyWork: keyWork
            }
        }, 'data');

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            localStorage.removeItem('appData');
            let index = layer.open({
                title: '添加版本',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: false,
                area: ['80%', '80%'],
                content: '/page/system/app/edit',
                end: function () {
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where: {
                            keyWork: keyWork
                        }
                    }, 'data');
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            let index=layer.confirm('确定要删除APP？', {btn: ['确定', '取消'], title: "提示"}, function () {
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                let params = {token: loginU.token, ids: ''};
                let ids = '';
                if (data.length > 0) {
                    for (let i = 0; i < data.length; i++) {
                        ids += data[i].appId + ',';
                    }
                    ids = ids.substring(0, ids.length - 1);
                }
                params.ids = ids;
                request.delList('app', params, function (result) {
                    if (result.code == 0) {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 500
                        }, function () {
                            //执行搜索重载
                            table.reload('currentTableId', {
                                page: {
                                    curr: 1
                                }
                                , where: {
                                    keyWork: keyWork
                                }
                            }, 'data');
                        });
                    } else {
                        layer.msg('保存失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
            });
        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        //console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        let data = obj.data;
        if (obj.event === 'share') {
            data.basePath = loginU.basePath;
            localStorage.setItem("appData", encryptLocalStorage(data, loginU.token));
            layer.open({
                title: 'APP下载二维码',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: false,
                area: ['50%', '80%'],
                content: '/page/system/app/share',
                end: function () {

                }
            });

        } else if (obj.event === 'delete') {
            let index=layer.confirm('确定要删除APP？', function (index) {
                console.log(JSON.stringify(obj.data.appId));
                layer.close(index);
                request.del('app', {token: loginU.token, id: obj.data.appId}, function (result) {
                    if (result.code == 0) {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 500
                        }, function () {
                            obj.del();
                        });
                    } else {
                        layer.msg('保存失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
            });
        }
    });

});